package com.fili.core.serial;

import com.fili.core.storage.DataItem;
import com.fili.core.storage.DataItemType;
import com.fili.core.storage.DataStore;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 19, 2006
 * Time: 10:39:29 PM
 */
public class SerialConsumer
{
    private DataStore dataStore;
    private DataItem dataItem;


    public DataStore getDataStore()
    {
        return dataStore;
    }

    public SerialConsumer(DataStore dataStore)
    {
        this.dataStore = dataStore;

        dataItem = new DataItem();
    }

    private int copyData(String data, int start, int len)
    {
        return Integer.parseInt(data.substring(start, start + len));
    }

    private boolean isISPMData(String data)
    {
        return data.charAt(1) >= '9';
    }

    public void receiveData(String data)
    {
        if (isISPMData(data))
        {
            dataItem.setValue(DataItemType.Index.PROBE_AIR_TEMP_1, copyData(data, 3, 4));
            dataItem.setValue(DataItemType.Index.PROBE_AIR_TEMP_2, copyData(data, 7, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_TEMP_1, copyData(data, 11, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_TEMP_2, copyData(data, 15, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_TEMP_3, copyData(data, 19, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_TEMP_4, copyData(data, 23, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_TEMP_5, copyData(data, 27, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_TEMP_6, copyData(data, 31, 4));
            dataItem.setValue(DataItemType.Index.AVG_AIR_TEMP, copyData(data, 35, 4));
            dataItem.setValue(DataItemType.Index.AVG_WOOD_TEMP, copyData(data, 40, 4));
        }
        else
        {
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_HUMID_1, copyData(data, 3, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_HUMID_2, copyData(data, 8, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_HUMID_3, copyData(data, 13, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_HUMID_4, copyData(data, 18, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_HUMID_5, copyData(data, 23, 4));
            dataItem.setValue(DataItemType.Index.PROBE_WOOD_HUMID_6, copyData(data, 28, 4));
            dataItem.setValue(DataItemType.Index.AVG_WOOD_HUMID, copyData(data, 33, 4));
            dataItem.setValue(DataItemType.Index.CURR_TEMP, copyData(data, 38, 4));
            dataItem.setValue(DataItemType.Index.CURR_UGL, copyData(data, 43, 4));
            dataItem.setValue(DataItemType.Index.CURR_HUMID, copyData(data, 48, 4));
            dataItem.setValue(DataItemType.Index.PHASE, copyData(data, 56, 1));
            dataItem.setValue(DataItemType.Index.WIDTH, copyData(data, 58, 1));
            dataItem.setValue(DataItemType.Index.SPECIES, copyData(data, 60, 2));
            dataItem.setValue(DataItemType.Index.WANTED_TEMP, copyData(data, 63, 4));
            dataItem.setValue(DataItemType.Index.WANTED_UGL, copyData(data, 68, 4));
            dataItem.setValue(DataItemType.Index.WANTED_HUMID, copyData(data, 73, 4));
        }
        dataStore.addItem(new DataItem(dataItem));
    }
}
